#include "glutContextTestCmd.h"
#include "openglContext.h"
#include <GL/glu.h>
#include <GL/glut.h>
#include <iostream>
using namespace std;



MStatus cmdGlutContext::doIt( const MArgList& args )
{
	MStatus stat;

	openglContextMaya contextForGLUT;
	contextForGLUT.dcInitialize();

	HANDLE glutThread = CreateThread(NULL, 0, glutContextShow, (LPVOID)paramG, 0, NULL);

	WaitForSingleObject(glutThread, 100);
	CloseHandle(glutThread);

	contextForGLUT.dcPurge();

	cout<<"glutTest"<<endl;

	return stat;
}

DWORD WINAPI cmdGlutContext::glutContextShow(LPVOID lpParam)
{
	struct paramGLUT *pParam = (struct paramGLUT *)lpParam; //change type?
	glutInit (& pParam->argc, pParam->argv);
	glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);
	glutInitWindowSize(500,500);
	glutInitWindowPosition(100,100);
	glutCreateWindow(pParam->argv[0]);
	glutDisplayFunc(Display);

	glutMainLoop();

	return 0;
}

void cmdGlutContext::Display()
{
	glClearColor(0.0,0.0,0.0,0.0);
	glClear(GL_COLOR_BUFFER_BIT);
	glColor3f(1,1,1);
	glOrtho(0.0,1.0,0.0,1.0,-1.0,1.0);

	glBegin(GL_POLYGON);
	glVertex3f(0.25,0.25,0);
	glVertex3f(0.75,0.25,0);
	glVertex3f(0.75,0.75,0);
	glVertex3f(0.25,0.75,0);
	glEnd();

	glFlush();
}